package com.mysky.client.mvp.drove;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealRootLayoutContentEvent;
import com.mysky.client.mvp.audioplayer.WidgetAudioPresenter;
import com.mysky.client.mvp.audioplayer.event.MDPlayEvent;
import com.mysky.client.mvp.audioplayer.event.MDPositionEvent;
import com.mysky.client.mvp.audioplayer.popup.PopupAudioPresenter;
import com.mysky.client.mvp.audioplayer.soudsample.SoundBundle;
import com.mysky.client.place.NameTokens;

public class DrovePresenter extends
		Presenter<DrovePresenter.MyView, DrovePresenter.MyProxy> {
	
	public static final Object SLOT_AUDIO = new Object();

	public interface MyView extends View {

		Button getSampleSongButton();

		Button getSampleSong50Button();
	}

	@ProxyStandard
	@NameToken(NameTokens.drove)
	public interface MyProxy extends ProxyPlace<DrovePresenter> {
	}

	@Inject
	public DrovePresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy) {
		super(eventBus, view, proxy);
	}

	@Inject
	PopupAudioPresenter popupAudioPresenter;

	@Inject
	WidgetAudioPresenter widgetAudioPresenter;

	@Override
	protected void onBind() {
		super.onBind();
	}

	@Override
	protected void revealInParent() {
		RevealRootLayoutContentEvent.fire(this, this);
	}
	
	@Inject 
	EventBus eventBus;
	
	@Override
	protected void onReveal() {
		super.onReveal();
		setInSlot(SLOT_AUDIO, widgetAudioPresenter);
		
		getView().getSampleSongButton().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				String url = SoundBundle.bundle.getDemo_mp3().getSafeUri()
						.asString();
				eventBus.fireEvent(new MDPlayEvent(url));
			}
		});
		getView().getSampleSong50Button().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				eventBus.fireEvent(new MDPositionEvent(50));
			}
		});
	}

}
